//
// * Copyright (c) 2002-2009 "Neo Technology,"
// *     Network Engine for Objects in Lund AB [http://neotechnology.com]
// *
// * This file is part of Neo4j.
// * 
// * Neo4j is free software: you can redistribute it and/or modify
// * it under the terms of the GNU Affero General Public License as
// * published by the Free Software Foundation, either version 3 of the
// * License, or (at your option) any later version.
// * 
// * This program is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// * GNU Affero General Public License for more details.
// * 
// * You should have received a copy of the GNU Affero General Public License
// * along with this program. If not, see <http://www.gnu.org/licenses/>.
// 
namespace org.neo4j.kernel.impl.nioneo.store
{


	using Test = junit.framework.Test;
	using TestCase = junit.framework.TestCase;
	using TestSuite = junit.framework.TestSuite;

	using AbstractStore = org.neo4j.kernel.impl.nioneo.store.AbstractStore;

	public class TestStore : TestCase
	{

		public TestStore(string testName) : base(testName)
		{
		}

		static void Main(string[] args)
		{
			junit.textui.TestRunner.run(suite());
		}

		public static Test suite()
		{
			TestSuite suite = new TestSuite(typeof(TestStore));
			return suite;
		}

		public virtual void setUp()
		{
		}

		public virtual void tearDown()
		{
		}

		public virtual void testCreateStore()
		{
			try
			{
				try
				{
					Store.createStore(null);
					fail("Null fileName should throw exception");
				}
				catch (IllegalArgumentException e)
				{ // good
				}
				Store store = Store.createStore("testStore.db");
				try
				{
					Store.createStore("testStore.db");
					fail("Creating existing store should throw exception");
				}
				catch (IllegalStateException e)
				{ // good
				}
				store.Close();
			}
			catch (IOException e)
			{
				e.printStackTrace();
				fail("" + e);
			}
			finally
			{
				File file = new File("testStore.db");
				if (file.exists())
				{
					assertTrue(file.delete());
				}
				file = new File("testStore.db.id");
				if (file.exists())
				{
					assertTrue(file.delete());
				}
			}
		}

		public virtual void testStickyStore()
		{
			try
			{
				Store.createStore("testStore.db").Close();
				java.nio.channels.FileChannel fileChannel = new java.io.RandomAccessFile("testStore.db", "rw").getChannel();
				fileChannel.truncate(fileChannel.size() - 2);
				fileChannel.Close();
				Store store = new Store("testStore.db");
				store.makeStoreOk();
				store.Close();
			}
			catch (IOException e)
			{
				fail("" + e);
			}
			finally
			{
				File file = new File("testStore.db");
				if (file.exists())
				{
					assertTrue(file.delete());
				}
				file = new File("testStore.db.id");
				if (file.exists())
				{
					assertTrue(file.delete());
				}
			}
		}

		public virtual void testClose()
		{
			try
			{
				Store store = Store.createStore("testStore.db");
				store.Close();
			}
			catch (IOException e)
			{
				fail("" + e);
			}
			finally
			{
				File file = new File("testStore.db");
				if (file.exists())
				{
					assertTrue(file.delete());
				}
				file = new File("testStore.db.id");
				if (file.exists())
				{
					assertTrue(file.delete());
				}
			}
		}

		private class Store : AbstractStore
		{
		// store version, each store ends with this string (byte encoded)
			private const string VERSION = "TestVersion v0.1";
			private const int RECORD_SIZE = 1;

//JAVA TO VB & C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public Store(String fileName) throws IOException
			public Store(string fileName) : base(fileName)
			{
			}

			protected internal virtual void initStorage()
			{
			}

			protected internal virtual void closeImpl()
			{
			}

			protected internal virtual bool fsck(bool modify)
			{
				return false;
			}

			public override int getRecordSize()
			{
				return RECORD_SIZE;
			}

			public virtual string getTypeAndVersionDescriptor()
			{
				return VERSION;
			}

//JAVA TO VB & C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public static Store createStore(String fileName) throws IOException
			public static Store createStore(string fileName)
			{
				createEmptyStore(fileName, VERSION);
				return new Store(fileName);
			}

			public virtual void flush()
			{
			}

			protected internal override void rebuildIdGenerator()
			{
			}
		}
	}
}